import area from './area.js'
import areaCode from './areaCode'

export function getServeAreaData(arr, allArr) {
  const lightAll = {
    first: [],
    second: []
  }
  allArr.forEach(item => {
    if (item.data.level == 1) {
      lightAll.first.push(item.data)
    } else if (item.data.level == 2) {
      lightAll.second.push(item.data)
    }
  })
  const second = []
  lightAll.second.forEach(item => {
    if (lightAll.first.findIndex(a => a.id == item.parentId) == -1) {
      second.push(item)
    }
  })

  const list = lightAll.first.concat(second)
  const list1 = []
  arr.forEach(item => {
    const index = list.findIndex(a => (a.id == item[0] || a.id == item[1]))
    if (index == -1) {
      list1.push(item)
    }
  })

  list1.forEach(item => {
    const index = allArr.findIndex(a => a.value == item[2])
    if (index != -1) {
      list.push(allArr[index].data)
    }
  })

  return list
}

export function getServeAreaData2(arr, allArr) {
  const lightAll = {
    first: [],
    second: [],
    third:{}
  }
  allArr.forEach(item => {
    if (item.data.level == 1) {
      lightAll.first.push(item.data)
    } else if (item.data.level == 2) {
      lightAll.second.push(item.data)
    }else {
      if (!lightAll.third[item.data.parentId]){
        lightAll.third[item.data.parentId] = []
      }
      lightAll.third[item.data.parentId].push(item.data)
    }
  })

  let list = []
  let seconds = {}
  lightAll.second.forEach(item=>{
    if (lightAll.third[item.id] &&  item.children.length == lightAll.third[item.id].length){
      //list.push(item)
      if (!seconds[item.parentId]){
        seconds[item.parentId] = []
      }
      seconds[item.parentId].push(item)
    }else if (!item.children){
      if (!seconds[item.parentId]){
        seconds[item.parentId] = []
      }
      seconds[item.parentId].push(item)
    }
  })
  lightAll.first.forEach(item=>{
    if (seconds[item.id] &&  item.children.length == seconds[item.id].length){
      list.push(item)
    }else if (!item.children){
      list.push(item)
    }
  })
  for (let key in seconds){
    let x = list.findIndex(a => a.id == key)
    if (x == -1){
      list = list.concat(seconds[key])
    }
  }
  // const second = []
  // lightAll.second.forEach(item => {
  //   if (lightAll.first.findIndex(a => a.id == item.parentId) == -1) {
  //     second.push(item)
  //   }
  // })
  //
  // const list = lightAll.first.concat(second)
  const list1 = []
  arr.forEach(item => {
    const index = list.findIndex(a => (a.id == item[0] || a.id == item[1]))
    if (index == -1) {
      list1.push(item)
    }
  })

  list1.forEach(item => {
    const index = allArr.findIndex(a => a.value == item[2])
    if (index != -1) {
      list.push(allArr[index].data)
    }
  })

  return list
}

export function aaa() {
  const a = {}

  area.forEach(p => {
    let list = []
    if (p.children) {
      p.children.forEach(city => {
        const arr = []
        if (city.children) {
          city.children.forEach(area1 => {
            arr.push([p.regionCode, city.regionCode, area1.regionCode])
          })
        } else {
          arr.push([p.regionCode, city.regionCode])
        }

        a[city.regionCode] = arr
        list = list.concat(arr)
      })
    } else {
      list.push([p.regionCode])
    }

    a[p.regionCode] = list
  })

  console.log(JSON.stringify(a))
}

export function getCascaderAreaData(arr) {
  let list = []

  for (let i = 0; i < arr.length; i++) {
    const item = arr[i]

    if (item.level == 3) {
      const pCode = item.regionCode.slice(0, 2) + '0000'
      const cCode = item.regionCode.slice(0, 4) + '00'

      list.push([pCode, cCode, item.regionCode])
    } else {
      list = list.concat(areaCode[item.regionCode])
    }
  }

  return list
}

export function getAreaName(item) {
  let str = item.regionName
  const index = area.findIndex(ar => ar.regionCode.slice(0, 2) == item.regionCode.slice(0, 2))
  if (index != -1) {
    const province = area[index]
    if (item.level == 2) {
      // str = province.regionName + "/" + item.regionName
    } else if (item.level == 3) {
      const idx = province.children.findIndex(a => a.regionCode == item.parentId)
      if (idx != -1) {
        const city = province.children[idx]
        // str = province.regionName + '/' + city.regionName + '/' + item.regionName
        str = city.regionName + '/' + item.regionName
      }
    }
  }

  return str
}

export function getScreenAreaData(arr) {
  const aaa = JSON.parse(JSON.stringify(area))
  const arr1 = aaa.filter(province => {
    const index = arr.findIndex(item => checkProvince(province, item))
    return index != -1
  })

  arr1.forEach(province => {
    if (province.children) {
      const arr2 = province.children.filter(city => { return arr.findIndex(item => checkCity(city, item)) != -1 })

      arr2.forEach(city => {
        if (city.children) {
          const arr3 = city.children.filter(area1 => { return arr.findIndex(item => checkArea(area1, item)) != -1 })

          city.children = arr3
        }
      })

      province.children = arr2
    }
  })

  return arr1
}

function checkProvince(province, item) {
  if (province.id == item.id) {
    return true
  } else if (item.regionCode.slice(0, 2) == province.regionCode.slice(0, 2)) {
    return true
  }
  return false
}

function checkCity(city, item) {
  if (city.id == item.id || city.parentId == item.id) {
    return true
  } else if (item.regionCode.slice(0, 4) == city.regionCode.slice(0, 4)) {
    return true
  }
  return false
}

function checkArea(area1, item) {
  if (area1.id == item.id) {
    return true
  } else if (item.regionCode == area1.regionCode.slice(0, 2) + '0000') {
    return true
  } else if (item.regionCode == area1.regionCode.slice(0, 4) + '00') {
    return true
  }
  return false
}

// 将多选的地区转成树结构
export function selectAreaTree(areaList) {
  console.log(areaList)
  const citys = {}
  const province = []
  var filterList = areaList.filter(p => p.level === 3)
  for (var i = 0; i < filterList.length; i++) {
    var items = {
      regionCode: filterList[i].value,
      regionName: filterList[i].label,
      label: filterList[i].label,
      value: filterList[i].value,
      checked: filterList[i].checked
    }
    if (!citys[filterList[i].parent.value]) {
      console.log(filterList[i])
      citys[filterList[i].parent.value] = filterList[i].parent
      citys[filterList[i].parent.value].children = []
    }
    citys[filterList[i].parent.value].children.push(items)
  }

  for (const key in citys) {
    var idx = province.findIndex(p => p.regionCode === citys[key].parent.value)
    var city = {
      regionCode: citys[key].value,
      regionName: citys[key].label,
      label: citys[key].label,
      value: citys[key].value,
      children: citys[key].children,
      checked: citys[key].checked
    }
    if (idx === -1) {
      var item = {
        regionCode: citys[key].parent.value,
        regionName: citys[key].parent.label,
        label: citys[key].parent.label,
        value: citys[key].parent.value,
        checked: citys[key].parent.checked,
        children: []
      }
      province.push(item)
      province[province.length - 1].children.push(city)
    } else {
      province[idx].children.push(city)
    }
  }
  return province
}
